package com.china08.yxyapi.api.manage.tec.office.classnotice;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 老师 班级通知 查询 该教师全部班级通知 该教师某一个班级的所有通知 该教师的发布的通知 该教师的发布的某一个班级的通知
 * 
 */
@CrossOrigin
@RestController("ClassnoticeSendSrhController")
public class ClassnoticeSendSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeClassRepository yxyNoticeClassRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/office/classnotice/searchType/{searchType}/classId/{classId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @PathVariable(value = "searchType") String searchType, // all my
            @PathVariable(value = "classId") String classId, // 0 or classId
            @RequestBody ReqModel reqModel) throws IllegalAccessException, InvocationTargetException {
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size, Direction.DESC, "createdDate");
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        // 当前教育局id
        String schoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String userId = getProfile(request, response).getId();
        Page<YxyNoticeClass> page = null;
        if ("all".equals(searchType)) {
            if ("0".equals(classId)) {
                // 是否毕业状态 0 正常 1 毕业
                // int status = 0;
                // 获取 当前学校下的当前用户 所带的所有 未毕业的班级列表
                // FIXED 云校库
                // Map<String, List<String>> map =
                // restTemplate.getForObject(yxyApiYxUrl +
                // "/comm/userclass/searchClassIdLst/" + schoolId + "/" +
                // userId, Map.class);
                // List<String> yxyClassIdLst = map.get("classIdLst");
                // Assert.isTrue(yxyClassIdLst != null && yxyClassIdLst.size()
                // != 0, "未查询到班级");
                // 查询全部所带班级下的通知
                // FIXED 业务库
                page = yxyNoticeClassRepository.findByClassIdInAndSchoolIdOrderByCreatedDateDesc(reqModel.classIdList,
                        schoolId, pageable);
            } else {
                // FIXED 业务库
                page = yxyNoticeClassRepository.findByClassIdAndSchoolIdOrderByCreatedDateDesc(classId, schoolId,
                        pageable);
            }
        }
        if ("my".equals(searchType)) {
            if ("0".equals(classId)) {
                // FIXED 业务库
                page = yxyNoticeClassRepository.findByUserIdAndSchoolIdOrderByCreatedDateDesc(userId, schoolId,
                        pageable);
            } else {
                // FIXED 业务库
                page = yxyNoticeClassRepository.findByUserIdAndSchoolIdAndClassIdOrderByCreatedDateDesc(userId,
                        schoolId, classId, pageable);
            }
        }
        long readManCount = 0;
        String readManUserNicknameStr = "";
        RespModel respModel = null;
        for (YxyNoticeClass yxyNoticeClass : page.getContent()) {
            String dateString = DateToolUtils.format(yxyNoticeClass.getCreatedDate(), "yyyy-MM-dd");
            respModel = new RespModel();
            respModel.date = dateString;
            respModel.setYxyNoticeClass(yxyNoticeClass);
            readManCount = yxyNoticeClass.getReadManCount(1);
            readManUserNicknameStr = yxyNoticeClass.getReadManUserNicknameStr(0, "、 ");
            respModel.setReadManCount(readManCount);
            respModel.setReadManUserNicknameStr(readManUserNicknameStr);
            respModelLst.add(respModel);
        }
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, page.getTotalElements());
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String searchType;
        public String classId;
        public List<String> classIdList;
    }

    protected static class RespModel {

        public String date;
        // 消息的全部信息
        private YxyNoticeClass yxyNoticeClass;
        // 已收到人数
        private long readManCount;
        // 未收到人员的姓名列表 多个用户昵称使用 、+ 空格 分隔
        private String readManUserNicknameStr;

        public long getReadManCount() {
            return readManCount;
        }

        public void setReadManCount(long readManCount) {
            this.readManCount = readManCount;
        }

        public String getReadManUserNicknameStr() {
            return readManUserNicknameStr;
        }

        public void setReadManUserNicknameStr(String readManUserNicknameStr) {
            this.readManUserNicknameStr = readManUserNicknameStr;
        }

        public YxyNoticeClass getYxyNoticeClass() {
            return yxyNoticeClass;
        }

        public void setYxyNoticeClass(YxyNoticeClass yxyNoticeClass) {
            this.yxyNoticeClass = yxyNoticeClass;
        }

    }
}
